package com.example.demospringboot.drwaing.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demospringboot.beanentity.User;
import com.example.demospringboot.daomapper.LoginMapper;
import com.example.demospringboot.daomapper.PlayerMapper;
import com.example.demospringboot.drwaing.entity.Picker;
import com.example.demospringboot.drwaing.entity.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("drawing")
public class DrawingController {
    // 每个部门的人数
    private static final int NUM_DEPT = 24;
    private List<Picker> res;
    @Autowired
    private  PlayerMapper playerMapper;
    @GetMapping("go")
    public List<Picker> go(){
        System.out.println(11111);
        int i = 0;
        while (i==0){
            i = b();
        }
        return res;
    }
    public  int b() {
        res = new ArrayList<>();
        try {

            // 创建总列表，包含所有部门人员
            List<Player> allPeople = new ArrayList<>();


            List<Player> otherPeople =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,0));
            List<Player> harbin = playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,1));
            List<Player> huhehaote =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,2));
            List<Player> lanzhou =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,3));
            List<Player> nanning =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,4));
            List<Player> qingzang = playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,5));


            allPeople.addAll(harbin);
            allPeople.addAll(huhehaote);
            allPeople.addAll(lanzhou);
            allPeople.addAll(nanning);
            allPeople.addAll(qingzang);
            allPeople.addAll(otherPeople);

            // 对所有人员进行随机打乱
            Collections.shuffle(allPeople);

            // 记录每个部门新的成员
            List<Player> harbinNew = new ArrayList<>();
            List<Player> huhehaoteNew = new ArrayList<>();
            List<Player> lanzhouNew = new ArrayList<>();
            List<Player> nanningNew = new ArrayList<>();
            List<Player> qingzangNew = new ArrayList<>();

            // 用于记录已经被分配的人员
            Set<Player> assignedPeople = new HashSet<>();

            // 按照要求分配每个部门24个人，并且确保不能在原部门
            assignPeople(harbin, harbinNew, allPeople,assignedPeople, NUM_DEPT);
            assignPeople(huhehaote, huhehaoteNew, allPeople,assignedPeople, NUM_DEPT);
            assignPeople(lanzhou, lanzhouNew, allPeople, assignedPeople,NUM_DEPT);
            assignPeople(nanning, nanningNew, allPeople, assignedPeople,NUM_DEPT);
            assignPeople(qingzang, qingzangNew, allPeople,assignedPeople, NUM_DEPT);
            // 输出结果
            List<Picker> harbinNewRes   = returnDepartment("哈尔滨局", harbinNew);
            List<Picker> huhehaoteNewRes  = returnDepartment("呼和浩特局", huhehaoteNew);
            List<Picker> lanzhouNewRes  = returnDepartment("兰州局", lanzhouNew);
            List<Picker> nanningNewRes  = returnDepartment("南宁局", nanningNew);
            List<Picker> qingzangNewRes  = returnDepartment("青藏公司", qingzangNew);
            res.addAll(harbinNewRes);
            res.addAll(huhehaoteNewRes);
            res.addAll(lanzhouNewRes);
            res.addAll(nanningNewRes);
            res.addAll(qingzangNewRes);
            return 1;
    }catch (Exception e){
        return 0;
    }

    }

    // 随机分配7个不在原部门的人员
    private  void assignPeople(List<Player> originalDepartment, List<Player> newDepartment, List<Player> allPeople, Set<Player> assignedPeople, int numDept) {
        List<Player> temp = new ArrayList<>(allPeople); // 创建副本避免修改原始列表
        temp.removeAll(originalDepartment); // 移除原部门人员

        // 过滤掉已经被分配过的人员
        temp.removeAll(assignedPeople);

        // 打乱剩余人员列表
        Collections.shuffle(temp);

        // 选取前numToAssign个未被分配且不在原部门的人员
        for (int i = 0; i < numDept; i++) {
            Player selectedPerson = temp.get(i);
            newDepartment.add(selectedPerson);
            assignedPeople.add(selectedPerson); // 标记该人员已经被分配
        }
    }

    // 输出每个部门的新成员
    private  List<Picker> returnDepartment(String departmentName, List<Player> department) {
        List<Picker> pickerList = new ArrayList<>();
        System.out.println(departmentName + " 新成员:");
        for (Player player : department) {
            Picker p = new Picker();
            p.setUnit(departmentName);
            p.setName(player.getName());
            p.setOriginalUnit(player.getUnit());
            pickerList.add(p);
            if (player.getUnit().equals(departmentName)){
                System.out.println(player.getUnit()+p.getName() + departmentName);
            }
        }
        return pickerList;
    }
}
